package at.fhj.itm11.fotorater.service;

import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import at.fhj.itm11.fotorater.data.PhotoRepository;
import at.fhj.itm11.fotorater.model.Photo;

@Stateless
public class PhotoRatingService {
	
	@Inject
	private Logger log;

	@Inject
	PhotoRepository phoRepo;

	@Inject
	private EntityManager em;
	
	public void likePhoto(Long id) {
		Photo p = phoRepo.findById(id);
		p.setLikes(p.getLikes() + 1);
		em.persist(p);
		
		log.info("Numer of likes of " + p.getName() + " (" + p.getCategory().getName() + ") has been raised to " +
		String.valueOf(p.getLikes()));
	}
	
	public void dislikePhoto(Long id) {
		Photo p = phoRepo.findById(id);
		p.setDislikes(p.getDislikes() + 1);
		em.persist(p);
		
		log.info("Numer of dislikes of " + p.getName() + " (" + p.getCategory().getName() + ") has been raised to " +
		String.valueOf(p.getDislikes()));
	}
}
